package com.easyj.admin.activiti.utils;


import java.util.Date;
import javax.annotation.PostConstruct;

import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.runtime.TaskRuntime;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.easyj.admin.activiti.service.TaskCommentService;
import com.easyj.admin.db.model.TaskComment;


/**
 * 流程\任务工具类
 * @author hjf
 *
 */
@Component
public class ProcessUtils {
	/**
	 * 后缀
	 */
	public  final static String  RoleSubffix= "@role";
	public  final static String  PositionSubffix= "@position";
	public  final static String  GroupSubffix= "@group";
	public  final static String  DeptSubffix= "@dept";
	
	//抄送
	public  final static String  NotifyFormKey= "NotifyForm";
	public  final static String  NotifyTaskKey= "NotifyTask";

	//类型
    public  final static String  COMMENT= "comment";
    
  
    @Autowired
    private TaskCommentService taskCommentService;
    
    @Autowired
    private TaskRuntime taskRuntime;
    
    private static TaskCommentService taskCommentSrv;
    
    private static TaskRuntime taskRuntimeSrv;
    
    @PostConstruct
	public void init(){
    	ProcessUtils.taskCommentSrv=taskCommentService;
    	ProcessUtils.taskRuntimeSrv=taskRuntime;
	}
    
   
    public static void addComment(Task task,String userId,String comment){
		TaskComment entity=new TaskComment();
		entity.setProcInstId(task.getProcessInstanceId());
		entity.setTaskId(task.getId());
		entity.setTaskName(task.getName());
		entity.setCreateTime(new Date());
		entity.setMessage(comment);
		entity.setType(COMMENT);
		entity.setUserId(userId);
		taskCommentSrv.addComment(entity);
		
    }
    
    
    public static void addComment(Task task,String userId,boolean passed,String comment){
        String str="";
		if(passed){
			str+="通过。"	;
		}else {
			str+="不通过。";
		}
		if(StringUtils.isNotBlank(comment)) {
		   str+=comment;
		}
		TaskComment entity=new TaskComment();
		entity.setProcInstId(task.getProcessInstanceId());
		entity.setTaskId(task.getId());
		entity.setTaskName(task.getName());
		entity.setCreateTime(new Date());
		entity.setMessage(str);
		entity.setType(COMMENT);
		entity.setUserId(userId);
		taskCommentSrv.addComment(entity);
		
    }
    
    
    public static void addComment(org.activiti.engine.task.Task task,String userId,boolean passed,String comment){
        String str="";
		if(passed){
			str+="通过。"	;
		}else {
			str+="不通过。";
		}
		if(StringUtils.isNotBlank(comment)) {
		   str+=comment;
		}
		TaskComment entity=new TaskComment();
		entity.setProcInstId(task.getProcessInstanceId());
		entity.setTaskId(task.getId());
		entity.setTaskName(task.getName());
		entity.setCreateTime(new Date());
		entity.setMessage(str);
		entity.setType(COMMENT);
		entity.setUserId(userId);
		taskCommentSrv.addComment(entity);
		
    }
	    
		
	    
    public static void addComment(String procInstId,String taskId,String taskName,String userId,boolean passed,String comment){
        String str="";
		if(passed){
			str+="通过。"	;
		}else {
			str+="不通过。";
		}
		if(StringUtils.isNotBlank(comment)) {
			   str+=comment;
		}
		TaskComment entity=new TaskComment();
		entity.setProcInstId(procInstId);
		entity.setTaskId(taskId);
		entity.setTaskName(taskName);
		entity.setCreateTime(new Date());
		entity.setMessage(str);
		entity.setType(COMMENT);
		entity.setUserId(userId);
		taskCommentSrv.addComment(entity);
		
    }
	/**
	 * 抄送...    
	 * @param parentTaskId
	 * @param assigneeNewTask
	 */
    public static void createNotifyTask( String parentTaskId, String userId) {
    	taskRuntimeSrv.create(
                TaskPayloadBuilder.create()
                        .withFormKey(NotifyFormKey)
                        .withName("抄送")
                        .withParentTaskId(parentTaskId)
                        .withAssignee(userId)
                        .withPriority(10)
                        .build());
    }	
	 
}

